﻿namespace YK.Console.Business.PackageMenuRoutes;

internal class SavePackageMenuRouteHandler(IRepository<PackageMenuRoute> _repo) : IRequestHandler<SavePackageMenuRouteRequest, bool>
{
    public async Task<bool> Handle(SavePackageMenuRouteRequest request, CancellationToken cancellationToken)
    {
        //清空套餐当前菜单
        await _repo.DeleteAsync(x => x.PackageId == request.PackageId, cancellationToken);

        if (request.MenuRouteIds.Any())
        {
            //保存
            var pkgMenuRoutes = request.MenuRouteIds.Select(x => new PackageMenuRoute
            {
                MenuRouteId = x,
                PackageId = request.PackageId,
            }).ToList();

            var result = await _repo.AddRangeAsync(pkgMenuRoutes, cancellationToken);
        }

        return true;
    }
}

internal class PackageMenuRouteSearchByPackagesHandler(IReadRepository<PackageMenuRoute> _repo) : IRequestHandler<PackageMenuRouteSearchByPackagesRequest, List<PackageMenuRouteOutput>>
{
    public Task<List<PackageMenuRouteOutput>> Handle(PackageMenuRouteSearchByPackagesRequest request, CancellationToken cancellationToken)
        => _repo.SimpleListAsync<PackageMenuRouteOutput>(x => request.PackageIds.Contains(x.PackageId) && x.MenuRoute.Enabled == EnabledStatusEnum.Enabled, cancellationToken);
}
